package com.ittest.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ittest.domain.Book;
import com.ittest.service.IBookservice;
import com.ittest.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;


@RestController
@RequestMapping("/books")
public class BookController {


    @Autowired
    private IBookservice bookService;

    @GetMapping
    public R getAll(){
        return new R(true,bookService.list());
    }


    @PostMapping
    public R save(@RequestBody Book book) throws IOException {

        if (book.getName().equals("123")) throw new IOException();
        boolean flag = bookService.save(book);
        return new R(flag ,flag ? "添加成功*.*":"添加失败#.#");
    }


    @PutMapping
    public R update(@RequestBody Book book){
        return new R(bookService.modify(book));

    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(bookService.delete(id));
    }


    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        System.out.println("test hot deploy...");
        System.out.println("test hot deploy...");
        return new R(true,bookService.getById(id));
    }

    //正常的分页查询
//    @GetMapping("{currentPage}/{pageSize}")
//    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
//        IPage<Book> page = bookService.getPage(currentPage, pageSize);
//        //如果当前页码值大于总页码值，那么重新执行操作，使用最大页面值当作当前页码值
//        if (currentPage > page.getPages()){
//            page = bookService.getPage((int) page.getPages(),pageSize);
//        }
//
//        return new R(true,page);
//    }

    //使用加上了条件的分页查询
    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Book book){
        IPage<Book> page = bookService.getPage(currentPage, pageSize,book);
        //如果当前页码值大于总页码值，那么重新执行操作，使用最大页面值当作当前页码值
        if (currentPage > page.getPages()){
            page = bookService.getPage((int) page.getPages(),pageSize,book);
        }

        return new R(true,page);
    }




}
